package org.ihelpuoo.io;

import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;

public class EncodedWriter extends Writer
{
	private Writer	w;
	private Charset	charset;

	public EncodedWriter(Writer w)
	{
		this.w = w;
	}

	public Charset getCharset()
	{
		return charset;
	}

	public void setCharset(Charset charset)
	{
		this.charset = charset;
	}

	@Override
	public void close() throws IOException
	{
		w.close();
	}

	@Override
	public void flush() throws IOException
	{
		w.flush();
	}

	@Override
	public void write(char[] cbuf, int off, int len) throws IOException
	{
		if (true || null == charset)
		{
			w.write(cbuf,off,len);
		}
		else
		{
			throw new RuntimeException("Don't support " + charset.name());
			// @ TODO ZZH: make it can encode String
			// ByteBuffer bb = charset.encode(CharBuffer.wrap(cbuf,off,len));
		}
	}
}
